package com.ll.domain.shop.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.app.api.req.AddShopReqDTO;
import com.ll.base.constant.LlConstants;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.dto.PageReqDTO;
import com.ll.base.dto.PageResultDTO;
import com.ll.base.enums.ResultCodeEnum;
import com.ll.base.exception.CustomRunTimeException;
import com.ll.base.util.ScalarUtil;
import com.ll.dal.po.GoodsPO;
import com.ll.dal.po.ShopPO;
import com.ll.dal.po.UserPO;
import com.ll.domain.shop.base.GoodsBaseService;
import com.ll.domain.shop.base.ShopBaseService;
import com.ll.domain.shop.biz.GoodsDomainService;
import com.ll.domain.shop.biz.ShopDomainService;
import com.ll.domain.shop.convert.GoodsConvert;
import com.ll.domain.shop.convert.ShopConvert;
import com.ll.domain.shop.dto.GoodsDTO;
import com.ll.domain.shop.dto.ShopDTO;
import com.ll.domain.user.base.UserBaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
@RequiredArgsConstructor
public class ShopDomainServiceImpl implements ShopDomainService {

    private final ShopBaseService shopBaseService;
    private final UserBaseService userBaseService;
    private final String SHOP =  "shop/";

    @Override
    public PageResultDTO<ShopDTO> page(PageReqDTO pageReq) {
        pageReq.initPage();
        IPage iPage = new Page(pageReq.getPageNo(), pageReq.getPageSize());
        iPage = shopBaseService.page(iPage, new LambdaQueryWrapper<ShopPO>());
        PageResultDTO<ShopPO> poPageResult = new PageResultDTO<>(iPage.getRecords(), iPage.getTotal(), pageReq);
        return PageResultDTO.PageBuildDTO(poPageResult, ShopConvert::PO2DTO);
    }

    @Override
    public ApiResultDTO addShop(AddShopReqDTO reqDTO, MultipartFile logo, MultipartFile logo1) {
        if(logo == null){
            throw new CustomRunTimeException(ResultCodeEnum.PARAM_NOT_EXIST);
        }
        UserPO userPO = userBaseService.getById(reqDTO.getCreator());
        ShopPO shopPO = ShopConvert.addShopReqDTO2PO(reqDTO);
        shopPO.setTenantId(userPO.getTenantId());
        shopBaseService.save(shopPO);

        shopBaseService.updateLogo(shopPO.getId(),  this.fileUtils(logo, String.valueOf(shopPO.getId())));

        if(logo1 != null){
            shopBaseService.updateLogo1(shopPO.getId(),  this.fileUtils(logo1,shopPO.getId() + "_1"));
        }


        return ApiResultDTO.success();
    }

    public String fileUtils(MultipartFile logo, String fileName){
        String[] split = logo.getOriginalFilename().split("\\.");
        fileName = fileName + ScalarUtil.DEFAULT_DIAN + split[split.length - 1];
        File file1 = new File(LlConstants.FILE_PATH + SHOP + fileName);
        try {
            logo.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return LlConstants.FILE_HTTP_URL_PREFIX + SHOP + fileName;
    }
}



